﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Project.Models;

namespace Project.Controllers
{
    public class ItemStocksController : Controller
    {
        private projektlaborEntities db = new projektlaborEntities();

        // GET: ItemStocks
        public async Task<ActionResult> Index()
        {
            var itemStock = db.ItemStock.Include(i => i.ItemCategory);
            return View(await itemStock.ToListAsync());
        }

        // GET: ItemStocks/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ItemStock itemStock = await db.ItemStock.FindAsync(id);
            if (itemStock == null)
            {
                return HttpNotFound();
            }
            return View(itemStock);
        }


        // GET: ItemStock/Details/5 a raktárcikk listázásához a RenderAction ezt hívja meg
        public ActionResult ItemStockDetails(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ItemStock itemStock = db.ItemStock.Find(id);
            if (itemStock == null)
            {
                return HttpNotFound();
            }
            return View(itemStock);
        }




        // GET: ItemStocks/Create
        public ActionResult Create()
        {
            ViewBag.ItemCategory_Id = new SelectList(db.ItemCategory, "ItemCategory_Id", "ItemCategoryName");
            return View();
        }

        // POST: ItemStocks/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Item_Id,ItemCategory_Id,ItemName,ItemUnit,ItemWidth,ItemHeight,ItemWeight,ItemPrice,ItemCurrency,ItemTransport,ItemIsFinished,ItemNote")] ItemStock itemStock)
        {
            if (ModelState.IsValid)
            {
                db.ItemStock.Add(itemStock);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ItemCategory_Id = new SelectList(db.ItemCategory, "ItemCategory_Id", "ItemCategoryName", itemStock.ItemCategory_Id);
            return View(itemStock);
        }

        // GET: ItemStocks/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ItemStock itemStock = await db.ItemStock.FindAsync(id);
            if (itemStock == null)
            {
                return HttpNotFound();
            }
            ViewBag.ItemCategory_Id = new SelectList(db.ItemCategory, "ItemCategory_Id", "ItemCategoryName", itemStock.ItemCategory_Id);
            return View(itemStock);
        }

        // POST: ItemStocks/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Item_Id,ItemCategory_Id,ItemName,ItemUnit,ItemWidth,ItemHeight,ItemWeight,ItemPrice,ItemCurrency,ItemTransport,ItemIsFinished,ItemNote")] ItemStock itemStock)
        {
            if (ModelState.IsValid)
            {
                db.Entry(itemStock).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ItemCategory_Id = new SelectList(db.ItemCategory, "ItemCategory_Id", "ItemCategoryName", itemStock.ItemCategory_Id);
            return View(itemStock);
        }

        // GET: ItemStocks/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ItemStock itemStock = await db.ItemStock.FindAsync(id);
            if (itemStock == null)
            {
                return HttpNotFound();
            }
            return View(itemStock);
        }

        // POST: ItemStocks/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            ItemStock itemStock = await db.ItemStock.FindAsync(id);
            db.ItemStock.Remove(itemStock);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
